import {
  getPassword,
  setPassword,
  getUserName,
  setUserName,
  getUserInfo,
  setUserInfo
} from "../../utils/store"

// index.js
const app = getApp()
const {
  store
} = app;
Page({
  useStore: true,
  data: {
    baseUrl: app.globalData.baseUrl,
    towerUrl: app.globalData.towerUrl,
    sapId: '',
    lastId: '',
    page: null,
    showPsw: false,
    pm14Flag: false
  },

  // 事件处理函数
  onLoad(query) {
    this.setData({
      sapId: getUserName(),
      lastId: getPassword(),
      scene: decodeURIComponent(query.scene),
      page: query.page,
      url: query.url,
      urlParams: query.urlParams
    })
  },
  submit() {

  },
  setSapId(e) {
    this.setData({
      'sapId': e.detail.value
    })
  },
  setIdLast(e) {
    this.setData({
      'lastId': e.detail.value
    })
  },
  toAwb() {
    wx.navigateTo({
      url: '/pages/awb/pound/index',
    })
  },
  gotoVisitor() {
    wx.navigateTo({
      url: '/pages/visitorLogin/visitorLogin?url='+this.data.url+'&urlParams='+this.data.urlParams,
    })
  },
  openWebView: function(event) {
    const url = event.currentTarget.dataset.url; // 获取按钮点击的 URL
    wx.navigateTo({
      url: `/pages/webview/index?url=${encodeURIComponent(url)}` // 导航到 webview 页面
    });
  },
  login() {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          app.api.getUser(that.data.sapId, that.data.lastId, res.code).then(res => {
            if (res.code == 200) {
              if (res.data) {
                console.log('登录完成之后的返回', res.data);
                setUserInfo(res.data);
                setUserName(that.data.sapId);
                setPassword(that.data.lastId);
                store.setState({
                  userInfo: res.data
                })

                //二维码 或者其他由于需要登录重新定位的页面
                if (that.data.url) {
                  console.log('that.data.url', that.data.url)
                  console.log('that.data.urlParams', that.data.urlParams)
                  wx.reLaunch({
                    url: `/${that.data.url}?urlParams=${that.data.urlParams}`
                  })
                  return
                }
                //公众号进入
                if ('riskList' == that.data.page) {
                  wx.reLaunch({
                    url: '/pages/risk-list/risk-list'
                  });
                  return;
                }
                if ('riskGz' == that.data.page) {
                  wx.reLaunch({
                    url: '/subPages/yinHuanGz/yinHuanGz'
                  });
                  return;
                }
                if ('moaTask' == that.data.page) {
                  wx.reLaunch({
                    url: '/pages/moa-task-list/moa-task-list'
                  });
                  return;
                }
                //扫码进入
                if (null != that.data.scene && 'undefined' != that.data.scene) {
                  let scenetemp = that.data.scene.toString();
                  if (scenetemp.startsWith('notice')) {
                    let qrId = scenetemp.replaceAll('notice?', '');
                    wx.reLaunch({
                      url: '/pages/risk-notice/risk-notice?id=' + qrId,
                    });
                    return;
                  }
                  if (scenetemp.startsWith('check')) {
                    let itemId = scenetemp.replaceAll('check?', '');
                    wx.reLaunch({
                      url: '/pages/check-item/check-item?itemId=' + itemId,
                    });
                    return;
                  }
                  if (scenetemp.startsWith('patrol')) {
                    let id = scenetemp.replaceAll('patrol?', '');
                    wx.reLaunch({
                      url: '/pages/patrol/patrol?id=' + id,
                    });
                    return;
                  }
                }
                wx.reLaunch({
                  url: '/pages/bigMenu/bigMenu',
                });
                return;
              } else {
                wx.showModal({
                  title: '提示',
                  content: '请输入正确的SAP账号和密码',
                  showCancel: false
                })
              }
            }
          })
        }
      }
    })

  },

  workdayLogin() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + '/staffInfo/wx/login?workdayId=' + that.data.sapId + '&psd=' + encodeURIComponent(that.data.lastId),
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('workday登录响应:', res.data);
        var result = res.data;
        if (200 == result.code) {
          setUserInfo(result.data);
          setUserName(that.data.sapId);
          setPassword(that.data.lastId);
          store.setState({
            userInfo: result.data
          });
          //二维码 或者其他由于需要登录重新定位的页面
          if (that.data.url) {
            console.log('that.data.url', that.data.url)
            console.log('that.data.urlParams', that.data.urlParams)
            wx.reLaunch({
              url: `/${that.data.url}?urlParams=${that.data.urlParams}`
            })
            return
          }
          wx.reLaunch({
            url: '/pages/menu2/menu2',
          });
          return;
        }else {
          wx.showModal({
            title: '提示',
            content: result.msg,
            showCancel: false
          })
        }
      }
    });

  },
  pm14Login() {
    var that = this;
    wx.request({
      url: that.data.towerUrl + '/pm14WxUser/wx/pm14Login?sapId=' + that.data.sapId + '&pin=' + encodeURIComponent(that.data.lastId),
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('pm14登录响应:', res.data);
        var result = res.data;
        if (200 == result.code) {
          setUserInfo(result.data);
          setUserName(that.data.sapId);
          setPassword(that.data.lastId);
          store.setState({
            userInfo: result.data
          });
          wx.reLaunch({
            url: '/pages/menu3/menu3',
          });
          return;
        }else {
          wx.showModal({
            title: '提示',
            content: result.msg,
            showCancel: false
          })
        }
      }
    });

  },
  showPassword(){
    console.log(11);
    this.setData({
      showPsw : !this.data.showPsw
    })
  },
  pm14(){
    this.setData({
      pm14Flag : true
    })
  },
  backpm14(){
    this.setData({
      pm14Flag : false
    })
  }
})